package com.kylin.cmsservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kylin.R;
import com.kylin.cmsservice.entity.CrmBanner;
import com.kylin.cmsservice.service.CrmBannerService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author kylin
 * @since 2020-07-18
 */
@RestController
@RequestMapping("/eduservice/banneradmin")
@CrossOrigin
public class BannerAdminController {

    @Autowired
    CrmBannerService crmBannerService;

    //1.分页查询banner
    @GetMapping("pageBanner/{page}/{limit}")
    @ApiOperation("分页查询banner")
    public R pageBanner(@PathVariable("page") long page,@PathVariable("limit") long limit){
        Page<CrmBanner> pageBanner = new Page<>(page,limit);
        crmBannerService.page(pageBanner, null);
        //记录
        List<CrmBanner> records = pageBanner.getRecords();
        //总数
        long total = pageBanner.getTotal();
        return R.ok().data("total",total).data("items",records);
    }

    //2.添加banner
    @PostMapping("/addBanner")
    @ApiOperation("添加banner")
    public R addBanner(@RequestBody CrmBanner crmBanner){

        crmBannerService.save(crmBanner);

        return  R.ok();
    }

    //3.修改banner
    @PutMapping("/update")
    @ApiOperation("修改banner")
    public R updateById (@RequestBody CrmBanner banner){

        crmBannerService.updateById(banner);

        return R.ok();
    }

    //4.删除banner
    @DeleteMapping("/remove{id}")
    @ApiOperation("删除banner")
    public R remove(@PathVariable String id){
        crmBannerService.removeById(id);
        return R.ok();
    }

    //5.根据Id查询Banner
    @GetMapping("/get/{id}")
    @ApiOperation("根据Id查询Banner")
    public R getBanner(@PathVariable String id ){

        CrmBanner crmBanner = crmBannerService.getById(id);

        return R.ok().data("item",crmBanner);
    }
}

